#!/usr/bin/python3

import sys
import subprocess
import datetime
import os

home = os.path.expanduser("~")

if len(sys.argv) != 2:
    print("""
    Run as:

        cin-debug <process-name>

    """)
    quit()

pname = sys.argv[1]
pid = subprocess.check_output(["pidof", pname]).decode('utf-8')
logfile = "%s/%s-%s.log" % (home, pname, datetime.datetime.now().strftime("%Y-%m-%d-%l:%M:%S"))


gdb_script = \
    """
set logging file %s
set logging on
set pagination off
attach %s

define hook-stop
    thread apply all bt
    quit
end

continue
""" % (logfile, pid)

os.system("echo '%s' | sudo gdb" % gdb_script)

quit()

